/******************************************************************
 * 文件：main.c
 * 功能：主函数入口
 * 日期：2018-02-16
 * 作者：zx
 * 版本：Ver.1.0 | 最初版本
 * 官博：http://fengmeitech.club
 * Copyright (C) 2017 zx. All rights reserved.
*******************************************************************/
#include "stdio.h"
#include "string.h"
#include "DELAY/Delay.h"
#include "ADC/ADC.h"
#include "LED/LED.h"
#include "NVIC/NVIC.h"
#include "IIC/IIC.h"
#include "OLED/OLED.h"
#include "RELAY/Relay.h"
#include "DHT11/DHT11.h"
#include "UART/uart.h"
#include "NetWork.h"
#include "TLink.h"
#include "misc.h"
#include "TIMx/TIMx.h"

void TimerInit(void);
void TIM3_IRQHandler(void);
int n=0;
DeviceSta_Strcture device = {0};

u16 DHT11_data; //存储DHT11传感器采集数据
int main(void)
	{
    u8 i = 0;

	

	/*初始化各外设*/ 
    initSysTick();  
	initADC();  //B1
    initUART();
    initUART2();//A2:TX , A3:RX
	initLED();  //C13
    initRelay();//A5:BEEP  ,A7:灌溉
    initDHT11();//A4
	initIIC();  //B10:SDA , B11:SCL
    initOLED();
	initNVIC(NVIC_PriorityGroup_2);//开启UART2的接收和空闲中断
	
	formatScreen(0x00);//清屏

    /*打印ESP8266启动信息到OLED*/
    if(initESP8266()!=0)
    {
        showString(0,0,"Init ok!",FONT_16_EN);
        showString(0,2,"Waiting...",FONT_16_EN);
    }else 
    {
        showString(0,0,"Init error!",FONT_16_EN);
    }
    while(connectAP(SSID,PWD)==0)
    {
        showString(0,2,"Waiting...",FONT_16_EN);
        
    }
				
        formatScreen(0x00);//清屏
        showString(0,0,"Init ok!",FONT_16_EN);
        showString(0,2,"Connect AP Ok!",FONT_16_EN);
    
    if(connectTlink()!=0)
    {
        showString(0,4,"Connect Link ok!",FONT_16_EN);
    }else
    {
        showString(0,4,"Connect Link no!",FONT_16_EN);
    }

    initTIMx(TIM3,7199,9999,TIM_IT_Update,ENABLE);
	TIM_ARRPreloadConfig(TIM3,ENABLE);

   /* 先采集一次，防止出现255 */
    
    DHT11_data = readDHT11();

    Delay_ms(2000);//让数据保持一段时间
    formatScreen(0x00);
    showCNString(32,0,"绿原农庄",FONT_16_CN);
    showString(0,2,"Hum :",FONT_16_EN);
    showString(0,4,"Temp:",FONT_16_EN);
    showString(0,6,"Lux :",FONT_16_EN);

 
    device.Humidity = DHT11_data>>8;
    device.Temperature = DHT11_data&0x00FF;
    i=0;
    while (1)
    {
        /*数据采集显示*/
        if(++i>3)
        {
            i = 0;
           
			showNumber(40,2,device.Humidity,DEC,3,FONT_16_EN);
            showNumber(40,4,device.Temperature,DEC,3,FONT_16_EN);
            showNumber(40,6,device.Lux,DEC,4,FONT_16_EN);
            //toggleLED1();

        }
        device.Lux = getConvValueAve(10,1000);
						
		Delay_ms(10);
    }
}








void TIM3_IRQHandler(void)
{
  
    
    
     
        if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
        {
            TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
            if(++n > 2)
            {
                n=0;
                DHT11_data = readDHT11();
                device.Humidity = DHT11_data>>8;
                device.Temperature = DHT11_data&0x00FF;
            }
            sendDeviceStatus(&device);
            toggleLED();
            
        }
    
}

